//Window kjører ved oppstart
//http://api.jquery.com/load-event/
$(window).load(function(){
	document.addEventListener('tizenhwkey', function(e) {
        if(e.keyName == "back")
            tizen.application.getCurrentApplication().exit();
    });
	
	console.log("Audio");
	//Først hente ut ID
	//http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript?rq=1
	// Då låner me litt kode frå internettet
	$.urlParam = function(name, url) {
	    if (!url) {
	     url = window.location.href;
	    }
	    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(url);
	    if (!results) { 
	        return undefined;
	    }
	    return results[1] || undefined;
	}
	//finne vår sangid
	var songid = $.urlParam('songid');
	console.log(songid)
	
	//Hente resten av vår data
	console.log("Leiter etter eine sangen");
	//OBS på filnavn under her	
	$.getJSON( "data/songs.json", function( json ) {

	  	//http://stackoverflow.com/questions/5288833/how-to-search-json-tree-with-jquery
	  	$.each(json.playlist.songs, function( key, value ) {
        if (value.id == songid) {
            // fann denn...
	        console.log("Fann "+value.title);
			//oppdater UI med riktig informasjon
			var nextid = value.id + 1;
		    oppdaterUI(value.title, value.artist, value.songlocation, value.cover, nextid)
		    
            return false; // stopper traversering
        }

	  	});
	 });

	// Custom knaster
	// http://www.alexkatz.me/html5-audio/building-a-custom-html5-audio-player-with-javascript/
	//evt
	//http://jsfiddle.net/LqM9D/1/
	window.player = document.getElementById('avspelar');
	
	document.getElementById('pButton').onclick = function () {
	    if (player.paused) {
	        player.play();
	        this.innerHTML = 'Pause';
	    } else {
	        player.pause();
	        this.innerHTML = 'Play';
	    }
	}
	console.log("running");

	addEventListener('timeupdate',function (){
	    curtime = parseInt(window.player.currentTime, 10);
	        $("#seek").attr("value", curtime);
    });

	$("#seek").bind("change", function() {
	        window.player.currentTime = $(this).val();
	        $("#seek").attr("max", window.player.duration);
    });
    
    //http://codepen.io/jtangelder/pen/lgELw
    //Gestures
	var myElement = document.getElementById('wrapper');
	console.log("running");
	// create a simple instance
	// by default, it only adds horizontal recognizers

	setUpGesture();
});

function setUpNextButton(nextid){
		var url= "audio.html?songid="+nextid
		$("#nextlink").attr("href", url);
}

function oppdaterUI(tittel, artist, songlocation, cover, nextid){
	 $( ".ui-title" ).html(tittel);
	 $( "#artist" ).html(artist);
	 http://api.jquery.com/attr/
	 $( "#avspelar" ).attr( "src", songlocation  );
	 $( "#cover" ).attr( "src", cover  );
	 setUpNextButton(nextid);


}

function setUpGesture(){
	var element = document.getElementById("wrapper");
	tau.event.enableGesture(element, new tau.event.gesture.Swipe(
	{
	orientation: "horizontal"
	}));

	element.addEventListener("swipe", function(e) 
	{
		if(e.detail.direction = 'left'){
			console.log('tilbage');
			window.location = 'index.html';
			//moar sexy men då må ein bytte til templating språket til TAU
			//tau.defaults.pageTransition = "slideup";
			//tau.changePage();<- fyll inn med pageid
			
		}
		//   console.log("swipe  = " + e.detail.direction);
	});
}
